package com.yunke.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.YunkeContactApply;
import com.yunke.service.YunkeContactApplyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 用户端联系方式申请控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/user/contact-apply")
@RequiredArgsConstructor
@Tag(name = "用户端联系方式申请", description = "用户端联系方式申请相关接口")
public class UserContactApplyController {

    private final YunkeContactApplyService contactApplyService;

    @PostMapping("/submit")
    @Operation(summary = "提交联系方式申请")
    public YunkeResult<String> submitApply(@Valid @RequestBody YunkeContactApply apply) {
        try {
            // TODO: 从当前登录用户获取userId
            Long userId = 1L;
            apply.setApplyUserId(userId);
            
            boolean success = contactApplyService.submitApply(apply);
            if (success) {
                return YunkeResult.success("申请提交成功");
            } else {
                return YunkeResult.error("申请提交失败");
            }
        } catch (Exception e) {
            log.error("提交联系方式申请失败", e);
            return YunkeResult.error("申请提交失败：" + e.getMessage());
        }
    }

    @GetMapping("/my-apply")
    @Operation(summary = "查询我的申请列表")
    public YunkeResult<IPage<YunkeContactApply>> getMyApplyList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer applyStatus) {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        IPage<YunkeContactApply> page = contactApplyService.getApplyPage(current, size, userId, null, applyStatus);
        return YunkeResult.success(page);
    }

    @GetMapping("/my-receive")
    @Operation(summary = "查询我收到的申请列表")
    public YunkeResult<IPage<YunkeContactApply>> getMyReceiveList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer applyStatus) {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        IPage<YunkeContactApply> page = contactApplyService.getApplyPage(current, size, null, userId, applyStatus);
        return YunkeResult.success(page);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "查询申请详情")
    public YunkeResult<YunkeContactApply> getApplyDetail(@PathVariable Long id) {
        YunkeContactApply apply = contactApplyService.getApplyById(id);
        if (apply == null) {
            return YunkeResult.error("申请不存在");
        }
        
        // TODO: 检查权限，只能查看自己相关的申请
        Long userId = 1L;
        if (!apply.getApplyUserId().equals(userId) && !apply.getTargetUserId().equals(userId)) {
            return YunkeResult.error("无权限查看此申请");
        }
        
        // 标记为已读
        if (apply.getTargetUserId().equals(userId) && !apply.getIsRead()) {
            contactApplyService.markAsRead(id);
        }
        
        return YunkeResult.success(apply);
    }

    @PostMapping("/approve/{id}")
    @Operation(summary = "同意申请")
    public YunkeResult<String> approveApply(@PathVariable Long id) {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        
        boolean success = contactApplyService.approveApply(id, userId);
        if (success) {
            return YunkeResult.success("申请已同意");
        } else {
            return YunkeResult.error("操作失败");
        }
    }

    @PostMapping("/reject/{id}")
    @Operation(summary = "拒绝申请")
    public YunkeResult<String> rejectApply(
            @PathVariable Long id,
            @RequestParam(required = false) String rejectReason) {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        
        boolean success = contactApplyService.rejectApply(id, userId, rejectReason);
        if (success) {
            return YunkeResult.success("申请已拒绝");
        } else {
            return YunkeResult.error("操作失败");
        }
    }

    @PostMapping("/cancel/{id}")
    @Operation(summary = "取消申请")
    public YunkeResult<String> cancelApply(@PathVariable Long id) {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        
        boolean success = contactApplyService.cancelApply(id, userId);
        if (success) {
            return YunkeResult.success("申请已取消");
        } else {
            return YunkeResult.error("操作失败");
        }
    }

    @GetMapping("/unread-count")
    @Operation(summary = "查询未读申请数量")
    public YunkeResult<Long> getUnreadCount() {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        long count = contactApplyService.getUnreadCount(userId);
        return YunkeResult.success(count);
    }

    @PostMapping("/mark-read/{id}")
    @Operation(summary = "标记为已读")
    public YunkeResult<String> markAsRead(@PathVariable Long id) {
        boolean success = contactApplyService.markAsRead(id);
        if (success) {
            return YunkeResult.success("标记成功");
        } else {
            return YunkeResult.error("操作失败");
        }
    }

    @PostMapping("/mark-all-read")
    @Operation(summary = "标记全部为已读")
    public YunkeResult<String> markAllAsRead() {
        // TODO: 从当前登录用户获取userId
        Long userId = 1L;
        boolean success = contactApplyService.markAllAsRead(userId);
        if (success) {
            return YunkeResult.success("标记成功");
        } else {
            return YunkeResult.error("操作失败");
        }
    }
} 